package com.api.action.v1;
/**
 * 终端登记房间号
 */

import com.admin.entity.Admin;
import com.admin.entity.Station;
import com.admin.entity.TGroup;
import com.admin.entity.Terminal;
import com.admin.service.IAdminService;
import com.admin.service.IStationService;
import com.admin.service.ITerminalService;
import com.api.service.v1.LoginApiService;
import com.api.service.v1.StationAPIService;
import com.bean.ResponseBean;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/api/v1")
public class LoginAPIAction {

    @Autowired
    private StationAPIService stationService;

    @Autowired
    private  IStationService iStationService;

    @Autowired
    private ITerminalService terminalService;

    @Autowired
    private LoginApiService loginApiService;

    @Autowired
    private IAdminService iAdminService;

    @Value("${applyStr}")
    private  String applyStr;


    /**
     * @param mac
     * @param id   组织ID
     * @param room 房间号
     * @return
     */


    @RequestMapping("/login")
    public ResponseBean login(String mac, String id, String room, String ip, String systemModel, String versionCode) {

        if (StringUtil.isNull(id) || StringUtil.isNull(mac) || StringUtil.isNull(room)) {
            return new ResponseBean(100, "参数出错", "");
        }
        //根据站点编号查找站点
        Station station = stationService.getStationByBH(id);
        if (station == null) {
            return new ResponseBean(400, "找不到站点", "");
        }

        //统计站点当前终端总数
        int count = loginApiService.countTerminal(station.getStationId());
        if (count >= station.getStationMaximumNumberOfTerminals()) {
            return new ResponseBean(100, "超过最大终端总数", "");
        }


        Terminal t = terminalService.getTerminalByMac(mac);
        if (t != null) {
            return new ResponseBean(300, "该Mac地址已注册", "");
        }

        int m = loginApiService.countRoom(station.getStationId(), room);
        if (m > 0) {
            return new ResponseBean(300, "房间号已存在", "");
        }


        Admin admin = iAdminService.getById(station.getStationAdminId());
        int authNum = admin.getAdminMoney();
        if (authNum < 1) {
            return new ResponseBean(500, "火币不足", "");
        }

        //客户端新增终端
        loginApiService.registTerminal(station,room,mac, ip, systemModel, versionCode);
        //站点已绑定终端数+1
        Integer num = station.getStaionCurrentNumberOTerminals();
        if (num==null)
        {
            num =0;
        }
        station.setStaionCurrentNumberOTerminals(num+1);
        iStationService.updateById(station);
        //沃币-1
        admin.setAdminMoney(authNum-1);
        iAdminService.updateById(admin);




        return new ResponseBean(200, "Success", "");
    }

    @RequestMapping("/getRoomAndID")
    public ResponseBean getRoomAndID(String mac) {
        Terminal t = terminalService.getTerminalByMac(mac);
        if (t == null)
        {
            return new ResponseBean(404, "终端不存在", "");
        }

        Station s = iStationService.getById(t.getTerminalStation());
        if (s == null)
        {
            return new ResponseBean(404, "站点不存在", "");
        }

        //获取房间和编号
        Map map =  loginApiService.getRoomAndId(s,t);

        return new ResponseBean(200, "success", map);
    }


    @RequestMapping("/shenQinJieBan")
    public ResponseBean shenQinJieBan(String mac) {

        Terminal terminal = terminalService.getTerminalByMac(mac);
        if(terminal != null)
        {
            terminal.setTerminalShenhe("未审核");
            terminalService.updateById(terminal);
            Map map = new HashMap();
            map.put("applyStr", applyStr);
            return new ResponseBean(200, "success", map);
        }
        Map map = new HashMap();
        map.put("applyStr", "申请解绑失败");
        return  new ResponseBean(500, "fail", map);
    }



}












